package com.marver.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Myco on 2017/6/12.
 * Description: 时间 日期操作类
 */
public class TimeUtils {

    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 获取昨日日期
     * @return  yestoday "2016-02-02"
     */
    public static String getYestoday(){
        Date yesToday;
        Date sysDate=new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(sysDate);
        calendar.add(calendar.DATE,-1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
        yesToday=calendar.getTime();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(yesToday);
        return dateString;
    }

    /**
     * 根据时间戳 获取 yyyy-MM-dd HH:mm:ss 格式 string时间
     * @param timeStamp
     * @return
     */
    public static String timeStampToDate(String timeStamp){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(timeStamp.length()==10){
            timeStamp =timeStamp+"000";
        }
        long lt = new Long(timeStamp);
        Date date = new Date(lt);
        String dateStr = format.format(date);
        return dateStr;
    }

    /**
     * 当月第一天
     * @return
     */
    public static String getFirstDay() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date theDate = calendar.getTime();

        GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
        gcLast.setTime(theDate);
        gcLast.set(Calendar.DAY_OF_MONTH, 1);
        String day_first = df.format(gcLast.getTime());
        StringBuffer str = new StringBuffer().append(day_first).append(" 00:00:00");
        return str.toString();

    }
}
